package com.qike.duola.controller;

import com.qike.duola.dto.ApprovalListDto;
import com.qike.duola.dto.ApprovalOperationDto;
import com.qike.duola.dto.ApprovalProcessSetDto;
import com.qike.duola.dto.WaitApprovalListDto;
import com.qike.duola.enums.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.ApprovalService;
import com.qike.duola.service.BaseService;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Api(description = "审批控制器")
@CrossOrigin
@RequestMapping("/approval")
public class ApprovalController {
    private static Logger logger = LoggerFactory.getLogger(ApprovalController.class);

    @Autowired
    private ApprovalService approvalService;




    /**
     * 审批模板列表
     *
     * @return
     */
    @ApiOperation("审批模板列表")
    @GetMapping("/approvalList")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ApprovalListVo.class)
    })
    public BaseResult ApprovalList(ApprovalListDto approvalListDto) {
        try {
            ApprovalListVo vo =   approvalService.ApprovalList(approvalListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 审批模板详情
     *
     * @return
     */
    @ApiOperation("审批模板详情")
    @GetMapping("/approvalInfo")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ApprovalInfoVo.class)
    })
    public BaseResult approvalInfo(Integer id) {
        try {
            ApprovalInfoVo vo =   approvalService.approvalInfo(id);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 审批流程设置
     *
     * @return
     */
    @ApiOperation("审批流程人员设置")
    @PostMapping("/approvalProcessSet")
    @ResponseBody
    public BaseResult approvalProcessSet(@RequestBody List<ApprovalProcessSetDto> approvalProcessSetDto ) {
        try {
              approvalService.approvalProcessSet(approvalProcessSetDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 我的审批列表
     *
     * @return
     */
    @ApiOperation("我的审批列表")
    @GetMapping("/waitApprovalList")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = WaitApprovalListVo.class)
    })
    public BaseResult waitApprovalList( WaitApprovalListDto waitApprovalListDto) {
        try {

            WaitApprovalListVo vo =   approvalService.waitApprovalList(waitApprovalListDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 审批同意/拒绝
     *
     * @return
     */
    @ApiOperation("审批同意/拒绝")
    @PostMapping("/approvalOperation")
    @ResponseBody
    public BaseResult approvalOperation(@RequestBody ApprovalOperationDto approvalOperationDto) {
        try {
           approvalService.approvalOperation(approvalOperationDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 审批进度查询
     *
     * @return
     */
    @ApiOperation("审批进度查询  id 原始订单id  approvalId 0工程审批 1 报价单审批 2 销售合同")
    @GetMapping("/approvalProgress")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ApprovalProgressVo.class)
    })
    public BaseResult approvalProgress(Integer id,Integer approvalId) {
        try {
            List<ApprovalProgressVo> vo =   approvalService.approvalProgress(id,approvalId);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 查询是否有设置审批流程
     *
     * @return
     */
    @ApiOperation("查询是否有设置审批流程")
    @GetMapping("/findProcess")
    @ResponseBody
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = ApprovalProgressVo.class)
    })
    public BaseResult findProcess(Integer id) {
        try {
            approvalService.findProcess(id);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }




}
